package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.service.UserService;
import com.metagurukul.metagranth.vo.User;

/**
 * Servlet implementation class ChangePasswordServlet
 */
public class ChangePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangePasswordServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = null;
		try {
			HttpSession session = request.getSession();
			String userName = (String) session.getAttribute("userName");
			String password = (String) request.getParameter("oldPassword");
			String msg = "";
			User user = new User();
			user.setUsername(userName);
			user.setPassword(password);
       
			User loggedUser = UserService.authentication(user);
			System.out.println(userName);
			System.out.println(password);
			if (loggedUser != null) {
				String newPassword = (String) request
				.getParameter("newPassword");
				String conNewPassword = (String) request
				.getParameter("confirmPassword");

				int status = UserService.changePassword(loggedUser,
						newPassword, conNewPassword);
				if (status == 0) {
					msg = "new password and confirm new password field is not filled";
					rd = request.getRequestDispatcher("changePassword.jsp");
				} else if (status == 1) {
					rd = request.getRequestDispatcher("changePassword.jsp");
					msg = "new password and confirm new password field not match";
				} else if (status == 2) {
					rd = request.getRequestDispatcher("home.jsp");
					msg = "credentilas has been changed";
				} else if (status == 3) {
					rd = request.getRequestDispatcher("changePassword.jsp");
					msg = "there is a internal problem.password not change";
				}

			} else {
				msg = "Old password is not right";
				rd = request.getRequestDispatcher("changePassword.jsp");
			}

			request.setAttribute("errorMsg", msg);
			rd.forward(request, response);
		} catch (Exception e) {
			request.setAttribute("errorMsg", e.getMessage());
			rd = request.getRequestDispatcher("errorPage.jsp");
			rd.forward(request, response);
		}
	}
}
